
;; =============================================================================
;; SYN:
;;
;; - there is no cons'es, no lispy dynamic typying... (1)
;; - all these things - (1) - expected to be implemented using self-cooked,
;;   injected type information
;;


;; =============================================================================
;; marked expander

(defparameter *marked-code*
  '(marked-code

    ;; defines markers
    (markers
     primitive-return)

    ;; primitive-return marker usage
    (primitive-return
     (func arg (type-spec arg-name))
     (func let (type name init-expr))
     (func set (var expr))
     (func call (func-name args))
     )

    (func block (block-expr)
     (loop for expr in block-expr collect
	   (progn
	     ())))

    
    )					; body
  )					; marked code


;; marked protocol that corresponds to CCNET protocol
(defparameter *marked-code-2*
  '(marked-code

    ;; protocol traits - set of constants
    (consts protocol-traits
     (max-message-size 0x100 (unit bytes))
     (min-message-size 6 (unit bytes))

     (protocol-token 2)
     (device-address 3)

     (timing-command-delay 100 (unit milliseconds))

     (message-code-pos 3 (unit bytes))

     (min-code-size 0)
     (max-code-size 0x100)
     ) ;; protocol traits

    ;; message codec - set of static functions to encode/decode messages
    (module message-codec

     (set default-scope protocol-traits)

     (func compose (buffer code data)
      ;; specification
      (returns int)
      (arguments (byte-arr buffer) (int code) (byte-arr data))

      ;; checks
      (if (< (length buffer) (get min-message-size))
	  (error "buffer given is too small"))

      (if (or (< code (get min-code-size)) (> code (get max-code-size)))
	  (error "${code} is out of range"))

      (let ((command-size 0)
	    (total-command-size (+ (get min-message-size) (length data)) const))
	;; checks
	(if (> total-command-size (get max-message-size))
	    (error "calculated command size is out of the desired bounds"))

	(if (> total-command-size (length buffer))
	    (error "command is too big to fit into the allocated buffer"))

	(set buffer command-size (get protocol-token))
	(set buffer (inc command-size) (get device-address))
	(set buffer (inc command-size) total-command-size)
	(set buffer (inc command-size) code)

	(for (data-byte data)
	     (set buffer (inc command-size) data-byte))

	
	) ;; let
      
      ) ;; compose
     ) ;; message codec
    
    )) ;; marked code 2

;; *marked-code-2*

(defun m-expand (marked-code))
